package asionreachel.cn.smarthomedemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import asionreachel.cn.smarthomedemo.R;
import asionreachel.cn.smarthomedemo.base.BaseAdapter;
import asionreachel.cn.smarthomedemo.data.CDeviceTypeInfo;
import asionreachel.cn.smarthomedemo.listener.OnRecyleItemListener;


/**
 * Created by SuperMa on 2017/6/12.
 */
public class GridAdapter extends BaseAdapter<GridAdapter.MyViewHolder , String>
{
    private ArrayList<CDeviceTypeInfo> mDatas;
    public GridAdapter(Context context) {
        super(context);
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_select_device, parent,
                false));
        return holder;
    }

    @Override
    public void onMyBindViewHolder(MyViewHolder holder, int position)
    {
        if( position < mDatas.size() )
        {
            holder.m_ivImg.setImageResource( mDatas.get(position).getIcon() );
            holder.m_tvName.setText(mDatas.get(position).getName());
            holder.m_tvCount.setText(mDatas.get(position).getCountInfo());
        }
    }

    @Override
    public int getItemCount()
    {
        return mDatas.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        LinearLayout m_lyType = null;
        ImageView m_ivImg = null;
        TextView m_tvName = null;
        TextView m_tvCount = null;

        public MyViewHolder(View view) {
            super(view);

            m_lyType = (LinearLayout) view.findViewById(R.id.ly_type);
            m_ivImg = (ImageView) view.findViewById(R.id.iv_img);
            m_tvName = (TextView) view.findViewById(R.id.tv_name);
            m_tvCount = (TextView) view.findViewById(R.id.tv_count);

            m_lyType.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onRecyleItemListener != null)
                        onRecyleItemListener.onItemClick(getAdapterPosition());
                }
            });
        }
    }

    private OnRecyleItemListener onRecyleItemListener;
    public void setOnRecyleItemListener(OnRecyleItemListener a_onRecyleItemListener)
    {
        this.onRecyleItemListener = a_onRecyleItemListener;
    }

    public void addData(int position , CDeviceTypeInfo a_DeviceTypeInfo) {
        if (position < mDatas.size())
        {
            mDatas.set(position , a_DeviceTypeInfo);
            notifyItemChanged(position);
        }
        else
        {
            mDatas.add(position, a_DeviceTypeInfo);
            notifyItemInserted(position);
            notifyItemRangeChanged( position, mDatas.size()-position );
        }
    }

    public void removeData(int position) {
        if (position < mDatas.size())
        {
            mDatas.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged( position, mDatas.size()-position+1 );
        }
    }

    public void setAdapterData(ArrayList<CDeviceTypeInfo> a_deviceTypeInfoList)
    {
        mDatas = a_deviceTypeInfoList;
    }

    public ArrayList<CDeviceTypeInfo> GetData()
    {
        return mDatas;
    }

    public CDeviceTypeInfo GetDataByIndex( int a_iIndex )
    {
        if( a_iIndex < 0 || a_iIndex >= mDatas.size() )
            return null;

        return mDatas.get( a_iIndex );
    }
}
